﻿app.controller('DicDataAddCtrl', function ($scope, $modalInstance, $interval, type, selected, selectedBranch, parentBranch, topData, dicSystem, dicData) {
    var setEdit = function () {
        $scope.parentData = parentBranch.data;
        $scope.dicData = selected[0];
    }
    var setAdd = function () {
        $scope.dicData = {};
        $scope.dicData.StatusCode = 1;
        if (selectedBranch) {
            setAddChild();
        } else {//没有选中过树的节点，添加顶级节点
            $scope.isAddTopNode = true;
            $scope.addTopNodeDisabled = true;
            setAddTop();
        }
    }
    var setAddTop = function () {
        $scope.parentData = topData;
        $scope.dicData.SystemInfo = topData.SystemInfo;
        $scope.dicData.ParentKey = topData.Key;
        $scope.dicData.LevelCode = Number(topData.LevelCode) + 1;
    }
    var setAddChild = function () {
        $scope.parentData = selectedBranch.data;
        $scope.dicData.SystemInfo = selectedBranch.data.SystemInfo;
        $scope.dicData.ParentKey = selectedBranch.data.Key;
        $scope.dicData.LevelCode = Number(selectedBranch.data.LevelCode) + 1;
    }

    $scope.isAddTopNode = false;
    var originalData;
    var lastData;
    $scope.message = '';
    var isEdit = (type == 'edit');
    if (isEdit) {
        $scope.type = '编辑';
        $scope.addTopNodeDisabled = true;
        originalData = (selected && selected.length > 0) ? DeepClone(selected[0]) : null;
        setEdit();
    } else {
        $scope.type = '添加';
        $scope.addTopNodeDisabled = false;
        setAdd();
    }

    $scope.addTopNode = function () {
        if ($scope.isAddTopNode) {
            setAddTop();
        } else {
            if (isEdit) {
                setEdit();
            } else {
                if (selectedBranch) {
                    setAddChild();
                }
            }
        }
    }

    $scope.getInputCode = function () {
        $scope.dicData.InputCode = GetFirstPinYinCode($scope.dicData.Value);
    };

    $scope.ok = function () {
        $modalInstance.close($scope.dicData);
    };

    $scope.cancel = function () {
        if (originalData) {
            selected[0] = originalData;
        }
        if (lastData) {
            $modalInstance.dismiss(lastData);
            return;
        }
        $modalInstance.dismiss();
    };

    $scope.okNext = function () {
        dicData.Add($scope.dicData).success(function (data, header, config, status) {
            $scope.message = '添加成功！（3）';
            $scope.seconds = 2;
            var message = $interval(function () {
                if ($scope.seconds <= 0) {
                    $interval.cancel(message);
                    $scope.message = '';
                } else {
                    $scope.message = '添加成功！（' + $scope.seconds + '）';
                    $scope.seconds--;
                }
            }, 1000);
            //第一个输入框获取焦点
            $scope.isFocus = true;
            //angular.alert('添加成功！');
            $scope.dicData.SerialNo = data;
            lastData = $scope.dicData;
            setAdd();
        }).error(function (data, header, config, status) {
            angular.alert('操作失败！');
        });
    };
});
